/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.content {
	import com.googlecode.milib.dbg.model.content.MLDBGTextModel;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGLoggerModel extends MLDBGTextModel {

		private var lastTitle:String;
		private var lastLogText:String='';
		private var countRepeats:uint=0;
		private var texts:Array=[];

		public function MLDBGLoggerModel() {
			
		}
		
		public function addLog(logText:String):void {
			if(lastLogText==logText){
				countRepeats++;
				broadcastEvent(event_TextChanged);
				return;
			}
//			if(lastLogText){
//				if(countRepeats>0){
//					text+='<br>'+lastLogText+' <b>*'+(countRepeats+1)+'</b>';
//				}else{
//					text+='<br>'+lastLogText;
//				}
//				
//			}
			if(lastLogText){
				texts.push(lastLogText);
				if(texts.length>100){
					while(texts.length>90){
						texts.shift();
					}
				}
			}
			countRepeats=0;
			lastLogText=logText;
			broadcastEvent(event_TextChanged);
		}
		
		override public function getText():String {
			if(countRepeats>0){
				return texts.join('\n')+'\n'+lastLogText+' <b>*'+(countRepeats+1)+'</b>';
			}else{
				return texts.join('\n')+'\n'+lastLogText;
			}
		}
		
		public function addLogWithTitle(title:String, logText:String):void {
			if(lastTitle!=title){
				lastTitle=title;
				addLog(title);
			}
			addLog('   '+logText);
		}
		
		public function clear():void {
			setText('');
		}
	}
}
